/**
 * Copyright (c) 2025 myons Inc. All rights reserved.
 */
package cn.zmwh.im.server.netty;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.zmwh.im.common.pojo.dto.IMRecvInfo;
import cn.zmwh.im.common.pojo.dto.IMUserInfo;
import cn.zmwh.im.common.pojo.dto.transfer.MessageGroup;
import cn.zmwh.im.common.pojo.dto.transfer.MessagePrivate;
import cn.zmwh.im.server.constant.IMCmdConstants;
import cn.zmwh.im.server.constant.IMRedisKey;
import cn.zmwh.im.server.modules.message.pojo.entity.ImGroupMessage;
import cn.zmwh.im.server.modules.message.pojo.entity.ImMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @describe: 补发消息 、
 * @author: dmzmwh 、
 * @time: 2025-06-27 16:54 、
 */
@Slf4j
@Service
public class ReissueMessageService {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 推送历史消息
     */
    public void messageSendHistory(List<ImMessage> list, IMUserInfo receiver) {
        if (CollUtil.isEmpty(list)) {
            return;
        }
        ArrayList<IMUserInfo> receivers = new ArrayList<>();
        receivers.add(receiver);
        List<IMRecvInfo> listMessage = new ArrayList<>();
        for (ImMessage imMessage : list) {
            Long sendId = imMessage.getSendId();

            MessagePrivate messagePrivate = new MessagePrivate();
            BeanUtil.copyProperties(imMessage, messagePrivate);
            IMRecvInfo imRecvInfo = new IMRecvInfo();
            imRecvInfo.setCmd(IMCmdConstants.PRIVATE_MESSAGE);
            imRecvInfo.setData(messagePrivate);
            imRecvInfo.setMessageId(imMessage.getMessageId());
            imRecvInfo.setSender(new IMUserInfo(sendId, null));

            imRecvInfo.setReceivers(receivers);
            listMessage.add(imRecvInfo);
        }
        String serverId = receiver.getServerId();
        String queueKey = String.join(":", IMRedisKey.IM_MESSAGE_REISSUE_QUEUE, serverId);
        redisTemplate.opsForList().rightPushAll(queueKey, listMessage);
    }

    /**
     * 推送群组历史消息
     *
     * @param list
     * @param receiver
     */
    public void messageSendGroupHistory(List<ImGroupMessage> list, IMUserInfo receiver) {
        if (CollUtil.isEmpty(list)) {
            return;
        }
        ArrayList<IMUserInfo> receivers = new ArrayList<>();
        receivers.add(receiver);
        List<IMRecvInfo> listMessage = new ArrayList<>();
        for (ImGroupMessage imGroupMessage : list) {
            Long sendId = imGroupMessage.getSendId();
            MessageGroup messageGroup = new MessageGroup();

            BeanUtil.copyProperties(imGroupMessage, messageGroup);
            String atUserIdsStr = imGroupMessage.getAtUserIds();
            if (StrUtil.isNotBlank(atUserIdsStr)) {
                List<Long> atUserIds = JSONUtil.toList(atUserIdsStr, Long.class);
                messageGroup.setAtUserIds(atUserIds);
            }
            IMRecvInfo imRecvInfo = new IMRecvInfo();
            imRecvInfo.setCmd(IMCmdConstants.GROUP_MESSAGE);
            imRecvInfo.setData(messageGroup);
            imRecvInfo.setMessageId(imGroupMessage.getMessageId());
            imRecvInfo.setSender(new IMUserInfo(sendId, null));

            imRecvInfo.setReceivers(receivers);
            listMessage.add(imRecvInfo);
        }
        String queueKey = String.join(":", IMRedisKey.IM_MESSAGE_REISSUE_GROUP_QUEUE, receiver.getServerId());
        redisTemplate.opsForList().rightPushAll(queueKey, listMessage);
    }
}
